package com.shinewind.info.controller;

import cn.hutool.core.util.BooleanUtil;
import com.shinewind.common.constant.messageConstant;
import com.shinewind.info.service.IModelCommentService;
import com.shinewind.info.service.IUserDetailService;
import com.shinewind.info.service.IUserInfoService;
import com.shinewind.pojo.dto.PageDto;
import com.shinewind.pojo.entity.ModelComment;
import com.shinewind.pojo.result.Result;
import com.shinewind.pojo.vo.CommentAllVo;
import com.shinewind.pojo.vo.ModelCommentVo;
import com.shinewind.pojo.vo.PageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/modelEvaluating")
@Api(value = "评论接口",description = "用于展示模型详情中的评论信息")
public class ModelCommentController {
    @Autowired
    private IModelCommentService iModelCommentService;
    @Autowired
    private IUserInfoService iUserInfoService;
    @Autowired
    private IUserDetailService iUserDetailService;

    @PostMapping("/detail/comment/list/{dId}")
    @ApiOperation(value = "评论信息",notes = "评论列表信息",response = Result.class)
    public PageVo<ModelCommentVo> getModelCommentListByPage(@PathVariable("dId") String dId,@RequestBody(required = false) PageDto pageDto){
        //根据模型详情id查询所有评论
        List<ModelComment> comments = iModelCommentService.getModelCommentsByPageANDId(dId,pageDto);
        List<ModelCommentVo> modelCommentVos = comments.stream().map(comment ->{
           ModelCommentVo modelCommentVo = ModelCommentVo.builder().build();
            BeanUtils.copyProperties(comment,modelCommentVo);
            //根据评论中的用户id查询用户昵称
            modelCommentVo.setNickName(iUserInfoService.getUserById(comment.getUId()).getNickName());
            //根据评论中的用户id查询用户头像
            modelCommentVo.setAvatar(iUserDetailService.getUserDetailById(comment.getUId()).getAvatar());
            return modelCommentVo;
        }).collect(Collectors.toList());
        Integer total = iModelCommentService.getTotalByDid(dId);
        return new PageVo<ModelCommentVo>(modelCommentVos, BooleanUtil.isTrue(modelCommentVos.size()>0),
                messageConstant.QUERY_SUCCESS,total);
    }

    @DeleteMapping("/detail/comment/delete/{cId}")
    @ApiOperation(value = "删除评论",notes = "删除评论",response = Result.class)
    public Result deleteCommentByCid(@PathVariable("cId") String cId){
        Boolean flag = iModelCommentService.deleteCommentBycId(cId);
        if(flag)
            return Result.success(true,
                    messageConstant.DELETE_SUCCESS);
        else
            return Result.fail(false,messageConstant.DELETE_ERROR);
    }
}
